package com.hy.admin.service.impl;

import com.hy.admin.dao.*;
import com.hy.admin.entity.MemberEntity;
import com.hy.admin.entity.MemberIncomeEntity;
import com.hy.admin.entity.MemberYangCoinEntity;
import com.hy.admin.enums.EnumImageRelatedType;
import com.hy.admin.enums.EnumMemberType;
import com.hy.admin.helper.response.PersonalResponse;
import com.hy.admin.service.PersonalService;
import com.hy.core.response.ServicesResult;
import org.apache.commons.httpclient.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by yangqq on 18/4/4.
 */
@Service
public class PersonalServiceImpl implements PersonalService {
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private MessageDao messageDao;
    @Autowired
    private QuestionDao questionDao;
    @Autowired
    private ImageDao imageDao;
    @Autowired
    private MemberIncomeDao memberIncomeDao;
    @Autowired
    private CourseOrderDao courseOrderDao;
    @Autowired
    private MemberYangCoinDao memberYangCoinDao;

    @Override
    public ServicesResult<PersonalResponse> center(Long memberId){
        ServicesResult<PersonalResponse> servicesResult = new ServicesResult<>();
        PersonalResponse personalResponse = new PersonalResponse();
        if (memberId == null || memberId <= 0){
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("请求参数有误");
            return servicesResult;
        }
        //获取用户对象
        MemberEntity memberEntity = memberDao.selectById(memberId);
        if (memberEntity == null){
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("用户不存在");
            return servicesResult;
        }
        //查询未读消息数量
        int messageNum = messageDao.selectUnreadCountByMemberId(memberId);
        if (memberEntity.getMemberType()!= null  && EnumMemberType.Teacher.getId() == memberEntity.getMemberType()) {
            //查询问答数量
            int questionNum = questionDao.selectCountByMemberId(memberId);
            personalResponse.setQuestionNum(questionNum);
        }
        //查询已购课程数量
        int courseNum = courseOrderDao.selectCountByMemberId(memberId);
        //查询氧币数量
        MemberYangCoinEntity memberYangCoin = memberYangCoinDao.selectByMemberId(memberId);
        //处理返回结果
        personalResponse.setNickName(memberEntity.getNickName());
        personalResponse.setCity(memberEntity.getCity());
        personalResponse.setMessageNum(messageNum);
        personalResponse.setCourseNum(courseNum);
        personalResponse.setCoinCount(memberYangCoin==null?0:memberYangCoin.getAmount());
        //查询用户头像
        List<String> imageList = imageDao.selectByRelatedIdAndType(memberEntity.getId(), EnumImageRelatedType.HEAD_IMAGE.getId());
        if(imageList != null && imageList.size() > 0){
            personalResponse.setImgPath(imageList.get(0));
        }

        //如果是教师查询教师收入
        if (memberEntity.getMemberType()!= null  && EnumMemberType.Teacher.getId() == memberEntity.getMemberType()){
            MemberIncomeEntity memberIncomeEntity = memberIncomeDao.selectByMemberId(memberId);
            if (memberIncomeEntity != null){
                personalResponse.setIncome(memberIncomeEntity.getAmount());
            }else{
                personalResponse.setIncome(0d);
            }
            personalResponse.setIndividualitySign(memberEntity.getIndividualitySign());
        }
        servicesResult.setResult(personalResponse);
        return servicesResult;
    }
}
